package com.cinematicketscaning.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.TextView
import com.cinematicketscaning.app.R

class MyExpandableListAdapter: BaseExpandableListAdapter {
    constructor(context: Context)
    var groupString = mutableListOf<String>("分类1", "分类2", "分类3", "分类4", "分类5")
    var childString = mutableListOf(mutableListOf("子分类1", "子分类2", "子分类3", "子分类4"), mutableListOf("子分类1", "子分类2", "子分类3", "子分类4"), mutableListOf("子分类1", "子分类2", "子分类3", "子分类4"), mutableListOf("子分类1", "子分类2", "子分类3", "子分类4"), mutableListOf("子分类1", "子分类2", "子分类3", "子分类4"))

    override fun getGroup(groupPosition: Int): Any {
        return groupString[groupPosition]
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View? {
        var groupHolder: GroupViewHolder? = null
        var myContentView: View? = convertView
        if (myContentView == null) {
            myContentView = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_ques_parent, parent, false)
            groupHolder = GroupViewHolder()
            groupHolder.tv = myContentView.findViewById(R.id.tv_content)
            myContentView.tag = groupHolder
        } else {
            groupHolder = myContentView.tag as GroupViewHolder
        }
        return myContentView
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return childString[groupPosition].size
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return childString[groupPosition][childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View? {
        var childHolder: ChildViewHolder? = null
        var myConvertView = convertView
        if (myConvertView == null) {
            myConvertView = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_ques_child, parent, false)
            childHolder = ChildViewHolder()
            childHolder.tv = myConvertView.findViewById(R.id.tv_content)
            myConvertView.tag = childHolder
        } else {
            childHolder = myConvertView.tag as ChildViewHolder
        }
        return myConvertView
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun getGroupCount(): Int {
        return groupString.size
    }

    class GroupViewHolder {
        var tv: TextView? = null
    }
    class ChildViewHolder {
        var tv: TextView? = null
    }
}